package com.shuwang.user.controller;

import com.shuwang.user.domain.UserMessageRequest;
import com.shuwang.user.enums.UserMessageType;
import com.shuwang.user.exception.BadRequestExitException;
import com.shuwang.user.exception.ConflictException;
import com.shuwang.user.exception.NotFoundException;
import com.shuwang.user.mapper.UserMessageMapper;
import com.shuwang.user.mapper.UserSigninMapper;
import com.shuwang.user.model.UserMessage;
import com.shuwang.user.model.UserMessageExample;
import com.shuwang.user.until.IDBuilder;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

@RestController
@RequestMapping(value = "usermessage")
@Api(value = "user_message",description = "用户留言API")
public class UserMessageController {

    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private UserSigninMapper userSigninMapper;

    @RequestMapping(value = "message",method = RequestMethod.POST)
    @ApiOperation(value = "用户新增留言")
    public UserMessage createUserMessage(@ApiParam(value = "请输入留言实体类") @RequestBody UserMessageRequest request){

        if (userSigninMapper.selectByPrimaryKey(request.getUserId()) == null){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        if ( !this.judgeUserMessageType(request.getType()) ){
            log.debug("type的值只能为"
                    +UserMessageType.message.getValue()+"("+UserMessageType.message.getName()+")、"
                    +UserMessageType.complaints.getValue()+"("+UserMessageType.complaints.getName()+")、"
                    +UserMessageType.ask.getValue()+"("+UserMessageType.ask.getName()+")、"
                    +UserMessageType.aftersales.getValue()+"("+UserMessageType.aftersales.getName()+")、"
                    +UserMessageType.lookingfor.getValue()+"("+UserMessageType.lookingfor.getName()+")");
            throw new BadRequestExitException("type的值只能为"
                    +UserMessageType.message.getValue()+"("+UserMessageType.message.getName()+")、"
                    +UserMessageType.complaints.getValue()+"("+UserMessageType.complaints.getName()+")、"
                    +UserMessageType.ask.getValue()+"("+UserMessageType.ask.getName()+")、"
                    +UserMessageType.aftersales.getValue()+"("+UserMessageType.aftersales.getName()+")、"
                    +UserMessageType.lookingfor.getValue()+"("+UserMessageType.lookingfor.getName()+")");
        }

        Long messageId = IDBuilder.CreateAnyId("204");
        UserMessage userMessage = new UserMessage();
        BeanUtils.copyProperties(request,userMessage);
        userMessage.setMessageId(messageId);
        userMessageMapper.insertSelective(userMessage);

        UserMessage success = userMessageMapper.selectByPrimaryKey(userMessage.getMessageId());
        if (success == null){
            log.debug("留言失败");
            throw new NotFoundException("留言失败");
        }

        return userMessage;
    }

    @RequestMapping(value = "message/messageId/{messageId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据messageId查询用户留言")
    public UserMessage viewUserMessageByMessageId(@ApiParam(value = "请输入messageId") @PathVariable Long messageId){

        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(messageId);
        if (userMessage == null){
            log.debug("留言信息不存在");
            throw new NotFoundException("留言信息不存在");
        }

        return userMessage;
    }

    @RequestMapping(value = "message/userId/{userId}",method = RequestMethod.GET)
    @ApiOperation(value = "根据userId查询用户留言")
    public List<UserMessage> viewUserMessageByUserId(@ApiParam(value = "请输入userId") @PathVariable Long userId){

        if (userSigninMapper.selectByPrimaryKey(userId) == null){
            log.debug("用户不存在");
            throw new NotFoundException("用户不存在");
        }

        UserMessageExample example = new UserMessageExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<UserMessage> userMessages = userMessageMapper.selectByExample(example);
        if (userMessages.isEmpty()){
            log.debug("留言信息不存在");
            throw new NotFoundException("留言信息不存在");
        }

        return userMessages;
    }

    @RequestMapping(value = "message/delete/{messageId}",method = RequestMethod.DELETE)
    @ApiOperation(value = "根据messageId查询用户留言")
    public UserMessage deleteUserMessageByMessage(@ApiParam(value = "请输入messageId") @PathVariable Long messageId){

        UserMessage userMessage = userMessageMapper.selectByPrimaryKey(messageId);
        if (userMessage == null){
            log.debug("留言信息不存在");
            throw new NotFoundException("留言信息不存在");
        }

        userMessageMapper.deleteByPrimaryKey(messageId);
        if (userMessageMapper.selectByPrimaryKey(messageId) != null){
            log.debug("留言删除失败");
            throw new ConflictException("留言删除失败");
        }

        return userMessage;
    }


    public boolean judgeUserMessageType(Integer type){

        if ( !type.equals(UserMessageType.message.getValue()) && !type.equals(UserMessageType.complaints.getValue()) &&
                !type.equals(UserMessageType.ask.getValue()) && !type.equals(UserMessageType.aftersales.getValue()) &&
                !type.equals(UserMessageType.lookingfor.getValue())){
            return false;
        }else{
            return true;
        }
    }

}
